<?php
namespace Home\Controller;
class LoginController extends HomeController
{
    protected $CAPTCHA_ID = "c6a8f518771d8ea164fd92890d5685b7";
    protected $PRIVATE_KEY = "5eff47a446c7f4958cef79f9e16b16c7";
    public function index(){


        $this->display('Index/index');
    }

    public function login(){

        $this->display('Login/login');
    }
    // 登录执行
    public function doLogin()
    {
        import("Org.Util.geetestlib");
        $GtSdk = new  \GeetestLib($this->CAPTCHA_ID,$this->PRIVATE_KEY);
        $user_id = $_SESSION['user_id'];
        if ($_SESSION['gtserver'] == 1) {
            $result = $GtSdk->success_validate($_POST['geetest_challenge'], $_POST['geetest_validate'], $_POST['geetest_seccode'], $user_id);
            if (!$result) {
                $this->error("都填写了吗？仔细检查");
            }
        }else{
            if (!$GtSdk->fail_validate($_POST['geetest_challenge'],$_POST['geetest_validate'],$_POST['geetest_seccode'])) {
                $this->error("都填写了吗？仔细检查");
            }
        }
        // 判断输入框是否为空
        if (empty($_POST)) {
            $this->error('您的输入为空!');
        }
        // 接收用户名和密码
        $name = I('post.name');
        $pwd =I('post.pwd');

        // 验证
        $user = M('user');
        $data = $user->where(array('name'=>$name))->find();
        if(!$data) {
            $this->error('用户名不存在!');
            exit;
        }


        // 验证密码
        if ($data['pwd'] != md5($pwd)) {
            $this->error('密码不正确');
            exit;
        }

        $_SESSION['user'] = $data;

        // 获取账号所对应的头像
        $info = M('headimg') ->where(array('id'=>$data['headimg_id']))->getField('img_200,img_150,img_40');
        foreach ($info as  $val) {
            foreach ($val as $k => $v) {
                $_SESSION['user'][$k] = $v;
            }
        }
        
       if(empty($_SESSION['url'])){
            $this->success('登陆成功',U('Index/index'));
       }else{
            $url=$_SESSION['url'];
            unset($_SESSION['url']);
            $this->success('登陆成功',U($url));

       }
    }

    // 退出登录
    public function logout()
    {
        // 清空session
        unset($_SESSION['user']);
        unset($_SESSION['comment']);
        unset($_SESSION['reply_comments']);
        // 跳转
        $this->redirect('Index/index');
    }

    public function register(){

        $this->display('Login/register');
    }

    public function insert(){

        //过滤数据
        $User = D('user');
        if (!$User->create()) {
            //如果创建失败  表示验证没有通过 输出错误提示信息
            $this->error($User->getError());
        } else {
            //验证通过
            if (D('user')->add() > 0) {
                $this->success('添加成功',U('Index/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

        public function checkRegister(){
            $name = $_POST['name'];
            if (M('user')->where(array('name'=>$name))->select()) {
                $this->ajaxReturn(0);
            } else {
                $this->ajaxReturn(1);
            }
            
        }
}